home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / sig / signals.c < prev    next >
C/C++ Source or Header  |  1991-03-14  |  40KB  |  1,370 lines

  1. /* 
  2.  * signals.c --
  3.  *
  4.  * Copyright 1988 Regents of the University of California.
  5.  * Permission to use, copy, modify, and distribute this
  6.  * software and its documentation for any purpose and without
  7.  * fee is hereby granted, provided that the above copyright
  8.  * notice appear in all copies.  The University of California
  9.  * makes no representations about the suitability of this
  10.  * software for any purpose.  It is provided "as is" without
  11.  * express or implied warranty.
  12.  *
  13.  * This contains routines that deal with Sprite signals.  See the man pages
  14.  * on signals for an explanation of the Sprite signaling facilities.  The
  15.  * only thing that is explained in these comments is the implementation of
  16.  * these facilities.
  17.  *
  18.  * SYNCHRONIZATION
  19.  *
  20.  * Whenever the signal state of a process is modified a monitor lock is
  21.  * grabbed.  When the signal state is looked at no locking is done. 
  22.  * It is assumed that there are two ways that the signal state will be
  23.  * looked at:
  24.  *
  25.  *      1) A process in the middle of executing a system call
  26.  *         will check to see if any signals are pending before waiting for
  27.  *         an extended period (i.e. waiting for a read to complete).  If
  28.  *         not then it will go to sleep until either a signal comes in or
  29.  *         the thing that it is waiting for completes.  This does not
  30.  *         require any synchronization on reading because the routine
  31.  *         which is used to put a process to sleep (see Sync_WaitEventInt)
  32.  *         will check for signals with the master lock down before the
  33.  *         process is put to sleep.  If there are signals pending, then
  34.  *         the sleep call will return immediately.  Otherwise if a signal
  35.  *         comes in after the process goes to sleep then it will be
  36.  *         awakened by the Sig_Send which calls Sync_WakeWaitingProcess which
  37.  *       synchronizes correctly with the Sync_Wait calls.  Thus there
  38.  *       is no way to miss a signal in this case.
  39.  *
  40.  *    2) A process is returning to user mode after trapping into the kernel
  41.  *       for some reason and it wants to see if signals are pending before
  42.  *       it returns.  In this case the trap handler (see Exc_Trap) will 
  43.  *       disable interrupts before checking to see if signals are pending.  
  44.  *       If they are then it will enable interrupts and process the signal.
  45.  *       Otherwise it will return to user mode with interrupts being enabled
  46.  *       on the return to user mode.  If a signal came in when 
  47.  *       interrupts were disabled then once interrupts are enabled the 
  48.  *       process will be interrupted and return back into the kernel.  
  49.  *       Likewise once the user process returns to user mode if a signal is 
  50.  *       delivered then the user process will be interrupted.  Interruption
  51.  *       is possible of course only on a multi-processor. Once interrupted it
  52.  *       will be forced back into the kernel where it will discover a
  53.  *       signal.  Thus a signal cannot be missed in this case either.
  54.  *
  55.  */
  56.  
  57. #ifndef lint
  58. static char rcsid[] = "$Header: /sprite/src/kernel/sig/RCS/signals.c,v 9.13 90/10/19 15:40:14 shirriff Exp $ SPRITE (Berkeley)";
  59. #endif not lint
  60.  
  61. #include <sprite.h>
  62. #include <stdlib.h>
  63. #include <sig.h>
  64. #include <sync.h>
  65. #include <dbg.h>
  66. #include <list.h>
  67. #include <proc.h>
  68. #include <procMigrate.h>
  69. #include <status.h>
  70. #include <sched.h>
  71. #include <sigInt.h>
  72. #include <rpc.h>
  73. #include <net.h>
  74. #include <vm.h>
  75. #include <bstring.h>
  76. #include <stdio.h>
  77.  
  78. #define    SigGetBitMask(sig) (1 << (sig - 1))
  79.  
  80. unsigned int     sigBitMasks[SIG_NUM_SIGNALS];
  81. int        sigDefActions[SIG_NUM_SIGNALS];
  82. int        sigCanHoldMask;
  83.  
  84. Sync_Lock    sigLock;
  85. Sync_Condition    signalCondition;
  86.  
  87. static void LocalSend _ARGS_((Proc_ControlBlock *procPtr, int sigNum, int code,
  88.             Address addr));
  89.  
  90.  
  91. /*
  92.  *----------------------------------------------------------------------
  93.  *
  94.  * Sig_Init --
  95.  *
  96.  *    Initialize the signal data structures.
  97.  *
  98.  * Results:
  99.  *    None.
  100.  *
  101.  * Side effects:
  102.  *    The set of bit masks and the set of default actions are set up.
  103.  *
  104.  *----------------------------------------------------------------------
  105.  */
  106.  
  107. void
  108. Sig_Init()
  109. {
  110.     int    i;
  111.  
  112.     Sync_LockInitDynamic(&sigLock, "Sig:sigLock");
  113.  
  114.     for (i = SIG_MIN_SIGNAL; i < SIG_NUM_SIGNALS; i++) {
  115.     sigBitMasks[i] = SigGetBitMask(i);
  116.     sigDefActions[i] = SIG_IGNORE_ACTION;
  117.     }
  118.  
  119.     sigDefActions[SIG_INTERRUPT]    = SIG_KILL_ACTION;
  120.     sigDefActions[SIG_KILL]        = SIG_KILL_ACTION;
  121.     sigDefActions[SIG_DEBUG]        = SIG_DEBUG_ACTION;
  122.     sigDefActions[SIG_ARITH_FAULT]    = SIG_DEBUG_ACTION;
  123.     sigDefActions[SIG_ILL_INST]     = SIG_DEBUG_ACTION;
  124.     sigDefActions[SIG_ADDR_FAULT]     = SIG_DEBUG_ACTION;
  125.     sigDefActions[SIG_BREAKPOINT]     = SIG_DEBUG_ACTION;
  126.     sigDefActions[SIG_TRACE_TRAP]     = SIG_DEBUG_ACTION;
  127.     sigDefActions[SIG_MIGRATE_TRAP]     = SIG_MIGRATE_ACTION;
  128.     sigDefActions[SIG_MIGRATE_HOME]     = SIG_MIGRATE_ACTION;
  129.     sigDefActions[SIG_SUSPEND]        = SIG_SUSPEND_ACTION;
  130.     sigDefActions[SIG_RESUME]        = SIG_KILL_ACTION;
  131.     sigDefActions[SIG_TTY_INPUT]    = SIG_SUSPEND_ACTION;
  132.     sigDefActions[SIG_PIPE]        = SIG_KILL_ACTION;
  133.     sigDefActions[SIG_TIMER]        = SIG_KILL_ACTION;
  134.     sigDefActions[SIG_TERM]        = SIG_KILL_ACTION;
  135.     sigDefActions[SIG_TTY_SUSPEND]    = SIG_SUSPEND_ACTION;
  136.     sigDefActions[SIG_TTY_OUTPUT]    = SIG_SUSPEND_ACTION;
  137.  
  138.     sigCanHoldMask = 
  139.           ~(sigBitMasks[SIG_ARITH_FAULT] | sigBitMasks[SIG_ILL_INST] |
  140.         sigBitMasks[SIG_ADDR_FAULT]  | sigBitMasks[SIG_KILL] |
  141.         sigBitMasks[SIG_BREAKPOINT]  | sigBitMasks[SIG_TRACE_TRAP] |
  142.         sigBitMasks[SIG_MIGRATE_HOME] | sigBitMasks[SIG_SUSPEND]);
  143. }
  144.  
  145.  
  146. /*
  147.  *----------------------------------------------------------------------
  148.  *
  149.  * Sig_ProcInit --
  150.  *
  151.  *    Initialize the signal data structures for the first process.
  152.  *
  153.  * Results:
  154.  *    None.
  155.  *
  156.  * Side effects:
  157.  *    Signal state initialized.
  158.  *
  159.  *----------------------------------------------------------------------
  160.  */
  161. void
  162. Sig_ProcInit(procPtr)
  163.     register    Proc_ControlBlock    *procPtr;
  164. {
  165.     procPtr->sigHoldMask = 0;
  166.     procPtr->sigPendingMask = 0;
  167.     bcopy((Address)sigDefActions,(Address)procPtr->sigActions,
  168.               sizeof(sigDefActions));
  169.     bzero((Address)procPtr->sigMasks,sizeof(procPtr->sigMasks)); 
  170.     bzero((Address)procPtr->sigCodes,sizeof(procPtr->sigCodes));
  171.     procPtr->sigFlags = 0;
  172. }
  173.  
  174.  
  175.  
  176. /*
  177.  *----------------------------------------------------------------------
  178.  *
  179.  * Sig_Fork --
  180.  *
  181.  *    Copy over the parents signal state into the child.
  182.  *
  183.  * Results:
  184.  *    None.
  185.  *
  186.  * Side effects:
  187.  *    Signal state copied from parent to child and pending mask cleared in
  188.  *    child.  Migration is held until the first return into user mode.
  189.  *
  190.  *----------------------------------------------------------------------
  191.  */
  192. void
  193. Sig_Fork(parProcPtr, childProcPtr)
  194.     register    Proc_ControlBlock    *parProcPtr;
  195.     register    Proc_ControlBlock    *childProcPtr;
  196. {
  197.     /*
  198.      * Copy the parent's signal state to the child.  Set up migration
  199.      * to be held initially.  On the first return to user mode, after
  200.      * signals are processed, migration will be reenabled.
  201.      */
  202.     childProcPtr->sigHoldMask = parProcPtr->sigHoldMask |
  203.         SigGetBitMask(SIG_MIGRATE_TRAP);
  204.     childProcPtr->sigPendingMask = 0;
  205.     bcopy((Address)parProcPtr->sigActions, 
  206.       (Address)childProcPtr->sigActions,
  207.       sizeof(childProcPtr->sigActions)); 
  208.     bcopy((Address)parProcPtr->sigMasks, 
  209.       (Address)childProcPtr->sigMasks,
  210.           sizeof(childProcPtr->sigMasks)); 
  211.     bzero((Address)childProcPtr->sigCodes,sizeof(childProcPtr->sigCodes));
  212.     childProcPtr->sigFlags = 0;
  213. }
  214.  
  215.  
  216. /*
  217.  *----------------------------------------------------------------------
  218.  *
  219.  * Sig_Exec --
  220.  *
  221.  *    Clear all signal handlers on exec.  Assumed called with the proc
  222.  *     table entry locked such that signals against this process are
  223.  *    prevented.
  224.  *
  225.  * Results:
  226.  *    None.
  227.  *
  228.  * Side effects:
  229.  *    All signal handlers are cleared and the pending mask is cleared.
  230.  *
  231.  *----------------------------------------------------------------------
  232.  */
  233. void
  234. Sig_Exec(procPtr)
  235.     Proc_ControlBlock    *procPtr;
  236. {
  237.     register    int    *actionPtr;
  238.     register    int    i;
  239.  
  240.     for (i = SIG_MIN_SIGNAL, actionPtr = &procPtr->sigActions[SIG_MIN_SIGNAL]; 
  241.      i < SIG_NUM_SIGNALS;
  242.      i++, actionPtr++) {
  243.     if (*actionPtr > SIG_SUSPEND_ACTION) {
  244.         /*
  245.          * The action contains a signal handler to call.  Reset back to
  246.          * the default action.
  247.          */
  248.         *actionPtr = sigDefActions[i];
  249.         procPtr->sigMasks[i] = 0;
  250.     }
  251.     }
  252.     procPtr->sigPendingMask = 0;
  253. }
  254.  
  255.  
  256. /*
  257.  *----------------------------------------------------------------------
  258.  *
  259.  * Sig_ChangeState --
  260.  *
  261.  *    Set the entire signal state of the process to that given.  When
  262.  *    setting the state verify that improper signals are not blocked or
  263.  *    ignored.
  264.  *
  265.  * Results:
  266.  *    None.
  267.  *
  268.  * Side effects:
  269.  *    The signal actions and hold mask will be set for the process.
  270.  *
  271.  *----------------------------------------------------------------------
  272.  */
  273.  
  274. ENTRY void
  275. Sig_ChangeState(procPtr, actions, sigMasks, pendingMask, sigCodes, holdMask)
  276.     register    Proc_ControlBlock    *procPtr;
  277.     int                    actions[];
  278.     register    int            sigMasks[];
  279.     int                    pendingMask;
  280.     int                    sigCodes[];
  281.     int                    holdMask;
  282. {
  283.     register    int    i;
  284.     register    int    *actionPtr;
  285.  
  286.     LOCK_MONITOR;
  287.  
  288.     for (i = SIG_MIN_SIGNAL, actionPtr = &actions[SIG_MIN_SIGNAL]; 
  289.      i < SIG_NUM_SIGNALS; 
  290.      i++, actionPtr++) {
  291.     if (i == SIG_KILL) {
  292.         continue;
  293.     }
  294.     procPtr->sigActions[i] = *actionPtr;
  295.     if (*actionPtr == SIG_IGNORE_ACTION) {
  296.         /*
  297.          * If is ignore action then make sure that is not one of the
  298.          * signals that cannot be ignored.  If not then remove the signal
  299.          * from the pending mask.
  300.          */
  301.         if (sigBitMasks[i] & sigCanHoldMask) {
  302.         pendingMask &= ~sigBitMasks[i];
  303.         } else {
  304.         procPtr->sigActions[i] = sigDefActions[i];
  305.         }
  306.     } else if (*actionPtr > SIG_NUM_ACTIONS) {
  307.         /*
  308.          * If greater than one of the actions then must be the address
  309.          * of a signal handler so store the signal mask.
  310.          */
  311.         procPtr->sigMasks[i] = sigMasks[i] & sigCanHoldMask;
  312.     }
  313.     }
  314.  
  315.     procPtr->sigPendingMask = pendingMask;
  316.  
  317.     procPtr->sigHoldMask = holdMask & sigCanHoldMask;
  318.     bcopy((Address) sigCodes, (Address) procPtr->sigCodes,
  319.           sizeof(procPtr->sigCodes));
  320.     procPtr->specialHandling = 1;
  321.  
  322.     UNLOCK_MONITOR;
  323. }
  324.  
  325.  
  326. /*
  327.  *----------------------------------------------------------------------
  328.  *
  329.  * Sig_UserSend --
  330.  *    Send a signal to a process.  Call the internal routine to do the
  331.  *    work.
  332.  *
  333.  * Results:
  334.  *    None.
  335.  *
  336.  * Side effects:
  337.  *    None.
  338.  *
  339.  *----------------------------------------------------------------------
  340.  */
  341.  
  342. ReturnStatus    
  343. Sig_UserSend(sigNum, pid, familyID)
  344.     int        sigNum;        /* The signal to send. */
  345.     Proc_PID    pid;        /* The id number of the process or process
  346.                    family. */
  347.     Boolean    familyID;    /* Whether the id is a process id or a process
  348.                    group id. */
  349. {
  350.     return(Sig_Send(sigNum, SIG_NO_CODE, pid, familyID, (Address)0));
  351. }
  352.  
  353.  
  354. /*
  355.  *----------------------------------------------------------------------
  356.  *
  357.  * LocalSend --
  358.  *
  359.  *    Send a signal to a process on the local machine.  It assumed that the
  360.  *    process is locked down when we are called.
  361.  *
  362.  * Results:
  363.  *    None.
  364.  *
  365.  * Side effects:
  366.  *    Signal pending mask and code modified.
  367.  *
  368.  *----------------------------------------------------------------------
  369.  */
  370.  
  371. ENTRY static void
  372. LocalSend(procPtr, sigNum, code, addr)
  373.     register    Proc_ControlBlock    *procPtr;
  374.     int                    sigNum;
  375.     int                    code;
  376.     Address                addr;
  377. {
  378.     int    sigBitMask;
  379.  
  380.     LOCK_MONITOR;
  381.  
  382.     /*
  383.      * Signals can't be sent to kernel processes unless the system is being
  384.      * shutdown since kernel processes never get the opportunity to handle
  385.      * signals.
  386.      */
  387.     if ((procPtr->genFlags & PROC_KERNEL) && !sys_ShuttingDown) {
  388.     UNLOCK_MONITOR;
  389.     return;
  390.     }
  391.  
  392.     if ((procPtr->sigActions[sigNum] == SIG_DEBUG_ACTION) &&
  393.     proc_KillMigratedDebugs && (procPtr->genFlags & PROC_FOREIGN)) {
  394.     /*
  395.      * Kill the process rather than letting it go silently into that
  396.      * good night (on the wrong machine).   Debugging migrated
  397.      * processes is nasty.  It would be nice if we could redirect
  398.      * the printf to the process's home node, too.
  399.      */
  400.     sigNum = SIG_KILL;
  401.     if (proc_MigDebugLevel > 1) {
  402.         printf("Warning: killing a migrated process that would have gone into the debugger, pid %x rpid %x uid %d.\n",
  403.             procPtr->processID, (int) procPtr->peerProcessID, 
  404.         procPtr->userID);
  405.  
  406.     }
  407.     }
  408.  
  409.     /*
  410.      * Only send the signal if it shouldn't be ignored and if it isn't
  411.      * a signal to migrate an unmigrated process.  (The latter can easily
  412.      * happen when signalling a process family to migrate home.)
  413.      */
  414.     if ((procPtr->sigActions[sigNum] != SIG_IGNORE_ACTION) &&
  415.     !((sigNum == SIG_MIGRATE_HOME) && (procPtr->peerHostID == NIL))) {
  416.     if (sigNum == SIG_RESUME) {
  417.         /* 
  418.          * Resume the suspended process.
  419.          */
  420.         Proc_ResumeProcess(procPtr, FALSE);
  421.     }
  422.     if (procPtr->sigActions[sigNum] == SIG_SUSPEND_ACTION &&
  423.            procPtr->state == PROC_SUSPENDED) {
  424.         /*
  425.          * Are sending a suspend signal to a process that is already
  426.          * suspended.  In this case just notify the parent that the 
  427.          * process has been suspended.  This is necessary because resume
  428.          * signals are sent by processes to debugged processes which do not
  429.          * really get resumed.  However, the signaling process will not
  430.          * be informed that the process it sent the signal to did not get
  431.          * resumed (SIG_RESUME works regardless whether it actually 
  432.          * resumes anything or not).  Thus a process may believe that
  433.          * a process is running even though it really isn't and it may
  434.          * send a suspend signal to an already suspended process.
  435.          *
  436.          * There is a potential race here between a process getting
  437.          * suspended and us checking here but it doesn't matter.  If
  438.          * it gets suspended after we check then the parent will get 
  439.          * notified anyway.
  440.          */
  441.         Proc_InformParent(procPtr, PROC_SUSPEND_STATUS);
  442.     } else if (sigNum != SIG_RESUME ||
  443.         procPtr->sigActions[sigNum] != SIG_KILL_ACTION) {
  444.         sigBitMask = sigBitMasks[sigNum];
  445.         procPtr->sigPendingMask |= sigBitMask;
  446.         procPtr->sigCodes[sigNum] = code;
  447.         procPtr->sigAddr = (int)addr;
  448.         if (procPtr->sigHoldMask & sigBitMask & ~sigCanHoldMask) {
  449.         /*
  450.          * We received a signal that was blocked but can't be blocked
  451.          * by users.  It only can be blocked if we are in the middle of
  452.          * executing a signal handler for the signal.  So we set things
  453.          * up to take the default action and make the signal unblocked
  454.          * so that we don't get an infinite loop of errors.
  455.          */
  456.         procPtr->sigHoldMask &= ~sigBitMask;
  457.         procPtr->sigActions[sigNum] = sigDefActions[sigNum];
  458.         }
  459.         procPtr->specialHandling = 1;
  460.         /*
  461.          * If the process is waiting then wake it up.
  462.          */
  463.         Sync_WakeWaitingProcess(procPtr);
  464.         if (sigNum == SIG_KILL || sigNum == SIG_MIGRATE_TRAP ||
  465.         sigNum == SIG_MIGRATE_HOME) {
  466.         if (sigNum == SIG_KILL && procPtr->state == PROC_NEW &&
  467.             (procPtr->genFlags & PROC_FOREIGN)) {
  468.             /*
  469.              * The process was only partially created.  We can't make
  470.              * it runnable so we have to reclaim it directly.
  471.              * Do this in the background so that
  472.              * Proc_DestroyMigratedProc has to wait for Sig_Send
  473.              * to unlock the process and we avoid a race condition.
  474.              */
  475.             Proc_CallFunc(Proc_DestroyMigratedProc,
  476.                   (ClientData) procPtr->processID, 0);
  477.         } else {
  478.             /*
  479.              * Resume the process so that we can perform the signal.
  480.              * If we're killing it, we tell Proc_ResumeProcess so it
  481.              * will even wake up a debugged process.
  482.              */
  483.             Proc_ResumeProcess(procPtr,
  484.                        (sigNum == SIG_KILL) ? TRUE : FALSE);
  485.         }
  486.         }
  487.     }
  488.     }
  489.     UNLOCK_MONITOR;
  490. }
  491.  
  492.  
  493. /*
  494.  *----------------------------------------------------------------------
  495.  *
  496.  * Sig_SendProc --
  497.  *
  498.  *    Store the signal in the pending mask and store the code for the given
  499.  *    process.  The code and addr are passed to the user interrupt
  500.  *    handler.  The code indicates the cause of the signal.  The addr
  501.  *    indicates the address of the fault.
  502.  *
  503.  *    NOTE: Assumes that we are called without the master lock down and
  504.  *    with the process locked.
  505.  *
  506.  * Results:
  507.  *    In the case of a local process, SUCCESS is returned.  If the process
  508.  *    is migrated, error conditions such as RPC_TIMEOUT may be returned.
  509.  *
  510.  * Side effects:
  511.  *    Signal pending mask and code modified.  If the process being signalled
  512.  *    is migrated, an RPC is sent.  If the process is local, the sched_Mutex
  513.  *    master lock is grabbed.
  514.  *
  515.  *----------------------------------------------------------------------
  516.  */
  517. ReturnStatus
  518. Sig_SendProc(procPtr, sigNum, code, addr)
  519.     register    Proc_ControlBlock *procPtr;
  520.     int                  sigNum;
  521.     int                  code;
  522.     Address              addr;
  523. {
  524.     ReturnStatus status;
  525.  
  526.     /*
  527.      * Make sure that the signal is in range.
  528.      */
  529.     if (sigNum < SIG_MIN_SIGNAL || sigNum >= SIG_NUM_SIGNALS) {
  530.     if (sigNum == 0) {
  531.         return(SUCCESS);
  532.     } else {
  533.         return(SIG_INVALID_SIGNAL);
  534.     }
  535.     }
  536.  
  537.     /*
  538.      * Handle migrated processes specially. There's a race condition
  539.      * when sending a signal to a migrated process, since it can
  540.      * migrate back to this host while we're doing it.  Therefore,
  541.      * if the problem was that the process didn't exist, check
  542.      * to see if it has migrated back to this host (it's no longer MIGRATED).
  543.      * We don't have to check for MIGRATING, since SigMigSend waits for
  544.      * a migration in progress to complete.   Also make sure that while the
  545.      * signal is sent and the process is unlocked, it processID doesn't change.
  546.      */
  547.     if (procPtr->state == PROC_MIGRATED ||
  548.         (procPtr->genFlags & PROC_MIGRATING)) {
  549.     Proc_PID processID;
  550.     processID = procPtr->processID;
  551.     status = SigMigSend(procPtr, sigNum, code, addr);
  552.     if (processID != procPtr->processID) {
  553.         return(status);
  554.     }
  555.     if ((status != PROC_INVALID_PID) ||
  556.         (procPtr->state == PROC_MIGRATED)) {
  557.         return(status);
  558.     }
  559.     }
  560.     if (procPtr->state == PROC_EXITING) {
  561.     return(PROC_INVALID_PID);
  562.     } else if (procPtr->state == PROC_NEW) {
  563.     if (procPtr->genFlags & PROC_FOREIGN && proc_MigDebugLevel > 0) {
  564.         printf("Warning: got signal for process %x before migration complete.\n",
  565.            procPtr->processID);
  566.     }
  567.     return(PROC_INVALID_PID);
  568.     } else {
  569.     LocalSend(procPtr, sigNum, code, addr);
  570.     return(SUCCESS);
  571.     }
  572. }
  573.  
  574.  
  575. /*
  576.  *----------------------------------------------------------------------
  577.  *
  578.  * Sig_Send --
  579.  *
  580.  *    Send a signal to a process.  This entails marking the signal into
  581.  *    the signal pending mask for the process and waking up the process
  582.  *    if it is asleep.   
  583.  *
  584.  * Results:
  585.  *    An error is the signal or the process id are invalid.  SUCCESS 
  586.  *    otherwise.
  587.  *
  588.  * Side effects:
  589.  *    The signal information in the proc table for the process that
  590.  *    is being sent the signal may be modified.
  591.  *
  592.  *----------------------------------------------------------------------
  593.  */
  594.  
  595. ReturnStatus    
  596. Sig_Send(sigNum, code, id, familyID, addr)
  597.     int        sigNum;        /* The signal to send. */
  598.     int        code;        /* The code that goes with the signal. */
  599.     Proc_PID    id;        /* The id number of the process or process
  600.                    family. */
  601.     Boolean    familyID;    /* Whether the id is a process id or a process
  602.                    group id. */
  603.     Address    addr;        /* The address of the fault */
  604. {
  605.     register    Proc_ControlBlock    *procPtr;
  606.     Proc_PCBLink            *procLinkPtr;
  607.     ReturnStatus            status;
  608.     List_Links                *familyList;
  609.     int                    userID;
  610.     int                    hostID;
  611.  
  612.     if (!Proc_ComparePIDs(id, PROC_MY_PID)) {
  613.     hostID = Proc_GetHostID(id);
  614.     if (hostID != rpc_SpriteID) {
  615.         /*
  616.          * Send a remote signal.
  617.          */
  618.         if (hostID == NET_BROADCAST_HOSTID ||
  619.         hostID >  NET_NUM_SPRITE_HOSTS || hostID < 0) {
  620.         return(PROC_INVALID_PID);
  621.         } else {
  622.         return(SigSendRemoteSignal(hostID, sigNum, code, id,
  623.                        familyID, addr));
  624.         }
  625.     }
  626.     }
  627.     /*
  628.      * Get the pointer to the control block if this is a valid process id.
  629.      */
  630.     if (!familyID) {
  631.     if (Proc_ComparePIDs(id, PROC_MY_PID)) {
  632.         procPtr = Proc_GetEffectiveProc();
  633.         if (procPtr == (Proc_ControlBlock *) NIL) {
  634.         panic("Sig_Send: procPtr == NIL\n");
  635.         }
  636.         Proc_Lock(procPtr);
  637.     } else {
  638.         procPtr = Proc_LockPID(id);
  639.         if (procPtr == (Proc_ControlBlock *) NIL) {
  640.         return(PROC_INVALID_PID);
  641.         }
  642.         if (!Proc_HasPermission(procPtr->effectiveUserID)) {
  643.         Proc_Unlock(procPtr);
  644.         return(PROC_UID_MISMATCH);
  645.         }
  646.     }
  647.     status = Sig_SendProc(procPtr, sigNum, code, addr);
  648.     Proc_Unlock(procPtr);
  649.     } else {
  650.     Proc_PID *pidArray;
  651.     int i;
  652.     int numProcs;
  653.     
  654.     status = Proc_LockFamily((int)id, &familyList, &userID);
  655.     if (status != SUCCESS) {
  656.         return(status);
  657.     }
  658.     if (!Proc_HasPermission(userID)) {
  659.             Proc_UnlockFamily((int)id);
  660.             return(PROC_UID_MISMATCH);
  661.         }
  662.  
  663.     /*
  664.      * Send a signal to everyone in the given family.  We do this
  665.      * by grabbing a list of process IDs and then sending the signals
  666.      * with the family not locked, to avoid deadlocks resulting from
  667.      * signals being sent with the family locked.
  668.      */
  669.  
  670.     numProcs = 0;
  671.     LIST_FORALL(familyList, (List_Links *) procLinkPtr) {
  672.         numProcs++;
  673.     }
  674.     pidArray = (Proc_PID *) malloc(numProcs * sizeof(Proc_PID));
  675.     i = 0;
  676.     LIST_FORALL(familyList, (List_Links *) procLinkPtr) {
  677.         procPtr = procLinkPtr->procPtr;
  678.         Proc_Lock(procPtr);
  679.         pidArray[i] = procPtr->processID;
  680.         Proc_Unlock(procPtr);
  681.         i++;
  682.         if (i > numProcs) {
  683.         panic("Sig_Send: process family changed size while locked.\n");
  684.         free((Address) pidArray);
  685.         return(FAILURE);
  686.         }
  687.     }
  688.     Proc_UnlockFamily((int)id);
  689.     for (i = 0; i < numProcs; i++) {
  690.         procPtr = Proc_LockPID(pidArray[i]);
  691.         if (procPtr == (Proc_ControlBlock *) NIL) {
  692.         /*
  693.          * Race condition: process got removed.
  694.          */
  695.         continue;
  696.         }
  697.         status = Sig_SendProc(procPtr, sigNum, code, addr);
  698.         Proc_Unlock(procPtr); 
  699.         if (status != SUCCESS) {
  700.         break;
  701.         }
  702.     }
  703.     free((Address) pidArray);
  704.     }
  705.  
  706.     return(status);
  707. }
  708.  
  709. typedef struct {
  710.     int        sigNum;
  711.     int        code;
  712.     Proc_PID    id;
  713.     Boolean        familyID;
  714.     int        effUid;
  715.     Address        addr;
  716. } SigParms;
  717.  
  718.  
  719. /*
  720.  *----------------------------------------------------------------------
  721.  *
  722.  * SigSendRemoteSignal --
  723.  *
  724.  *    Send a signal to a process on a remote machine.
  725.  *
  726.  * Results:
  727.  *    Return the status from the remote machine.
  728.  *
  729.  * Side effects:
  730.  *    None.
  731.  *
  732.  *----------------------------------------------------------------------
  733.  */
  734. ReturnStatus    
  735. SigSendRemoteSignal(hostID, sigNum, code, id, familyID, addr)
  736.     int        hostID;        /* Host to send message to. */
  737.     int        sigNum;        /* Signal to send. */
  738.     int        code;        /* Code to send. */
  739.     Proc_PID    id;        /* ID to send it to. */
  740.     Boolean    familyID;    /* TRUE if are sending to a process family. */
  741.     Address    addr;        /* Address of signal. */
  742. {
  743.     SigParms        sigParms;
  744.     Rpc_Storage        storage;
  745.     Proc_ControlBlock    *procPtr;
  746.  
  747.     sigParms.sigNum = sigNum;
  748.     sigParms.code = code;
  749.     sigParms.id = id;
  750.     sigParms.familyID = familyID;
  751.     procPtr = Proc_GetEffectiveProc();
  752.     sigParms.effUid = procPtr->effectiveUserID;
  753.     sigParms.addr = addr;
  754.  
  755.     storage.requestParamPtr = (Address)&sigParms;
  756.     storage.requestParamSize = sizeof(sigParms);
  757.     storage.requestDataPtr = (Address)NIL;
  758.     storage.requestDataSize = 0;
  759.     storage.replyParamPtr = (Address)NIL;
  760.     storage.replyParamSize = 0;
  761.     storage.replyDataPtr = (Address)NIL;
  762.     storage.replyDataSize = 0;
  763.  
  764.     return(Rpc_Call(hostID, RPC_SIG_SEND, &storage));
  765.  
  766. }
  767.  
  768.  
  769. /*
  770.  *----------------------------------------------------------------------
  771.  *
  772.  * Sig_RpcSend --
  773.  *
  774.  *    Stub to handle a remote signal RPC.
  775.  *
  776.  * Results:
  777.  *    SUCCESS.
  778.  *
  779.  * Side effects:
  780.  *    Reply is sent.
  781.  *
  782.  *----------------------------------------------------------------------
  783.  */
  784. /*ARGSUSED*/
  785. ReturnStatus    
  786. Sig_RpcSend(srvToken, clientID, command, storagePtr)
  787.     ClientData          srvToken;    /* Handle on server process passed to
  788.                       * Rpc_Reply */
  789.     int          clientID;    /* Sprite ID of client host */
  790.     int          command;    /* Command identifier */
  791.     register Rpc_Storage *storagePtr;    /* The request fields refer to the 
  792.                      * request buffers and also indicate 
  793.                      * the exact amount of data in the 
  794.                      * request buffers.  The reply fields 
  795.                      * are initialized to NIL for the
  796.                       * pointers and 0 for the lengths.  
  797.                      * This can be passed to Rpc_Reply */
  798. {
  799.     SigParms        *sigParmsPtr;
  800.     ReturnStatus    status;
  801.     Proc_ControlBlock    *procPtr;
  802.     int            effUid;
  803.  
  804.     sigParmsPtr = (SigParms *) storagePtr->requestParamPtr;
  805.     procPtr = Proc_GetCurrentProc();
  806.     effUid = procPtr->effectiveUserID;
  807.     procPtr->effectiveUserID = sigParmsPtr->effUid;
  808.     status = Sig_Send(sigParmsPtr->sigNum, sigParmsPtr->code, sigParmsPtr->id,
  809.               sigParmsPtr->familyID, sigParmsPtr->addr);
  810.     procPtr->effectiveUserID = effUid;
  811.     Rpc_Reply(srvToken, status, storagePtr, (int(*)())NIL, (ClientData)NIL);
  812.     return(SUCCESS);
  813. }
  814.  
  815.  
  816. /*
  817.  *----------------------------------------------------------------------
  818.  *
  819.  * Sig_SetHoldMask --
  820.  *
  821.  *    Set the signal hold mask for the current process.  Return the
  822.  *    old mask.  No synchronization required since the only process
  823.  *    that can modify the hold mask is this process.
  824.  *
  825.  * Results:
  826.  *    Error if the place to store the old mask is invalid,  SUCCESS
  827.  *    otherwise.
  828.  *
  829.  * Side effects:
  830.  *    None.
  831.  *
  832.  *----------------------------------------------------------------------
  833.  */
  834.  
  835. ReturnStatus    
  836. Sig_SetHoldMask(newMask, oldMaskPtr)
  837.     int    newMask;    /* Mask to set the hold mask to. */
  838.     int    *oldMaskPtr;    /* Where to store the old mask. */
  839. {
  840.     register    Proc_ControlBlock    *procPtr;
  841.  
  842.     /*
  843.      * Get out the old mask value and store the new one.
  844.      */
  845.  
  846.     procPtr = Proc_GetActualProc();
  847.  
  848.     if (oldMaskPtr != USER_NIL) {
  849.     if (Vm_CopyOut(sizeof(procPtr->sigHoldMask), 
  850.                (Address) &(procPtr->sigHoldMask),
  851.                (Address) oldMaskPtr) != SUCCESS) {
  852.         return(SYS_ARG_NOACCESS);
  853.     }
  854.     }
  855.  
  856.     procPtr->sigHoldMask = newMask & sigCanHoldMask;
  857.     procPtr->specialHandling = 1;
  858.  
  859.     return(SUCCESS);
  860. }
  861.  
  862.  
  863. /*
  864.  *----------------------------------------------------------------------
  865.  *
  866.  * Sig_SetAction --
  867.  *
  868.  *    Set the action for a particular signal.
  869.  *
  870.  * Results:
  871.  *    Error if the action, signal, or handler is invalid.
  872.  *
  873.  * Side effects:
  874.  *    The sigAction and sigMasks fields may be modified for the
  875.  *    particular signal.
  876.  *
  877.  *----------------------------------------------------------------------
  878.  */
  879.  
  880. ReturnStatus    
  881. Sig_SetAction(sigNum, newActionPtr, oldActionPtr)
  882.     int        sigNum;           /* The signal for which the action is to be 
  883.                   set. */
  884.     Sig_Action    *newActionPtr; /* The actions to take for the signal. */
  885.     Sig_Action    *oldActionPtr; /* The action that was taken for the signal. */
  886. {
  887.     Proc_ControlBlock    *procPtr;
  888.     Address        dummy;
  889.     Sig_Action        action;
  890.  
  891.     /*
  892.      * Make sure that the signal is in range.
  893.      */
  894.     if (sigNum < SIG_MIN_SIGNAL || sigNum >= SIG_NUM_SIGNALS || 
  895.     sigNum == SIG_KILL || sigNum == SIG_SUSPEND) {
  896.     return(SIG_INVALID_SIGNAL);
  897.     }
  898.  
  899.     procPtr = Proc_GetActualProc();
  900.  
  901.     /* 
  902.      * Copy out the current action.  There are two cases:
  903.      *
  904.      *    1) The current action really contains a handler to call.  Thus
  905.      *         the current action is SIG_HANDLE_ACTION.
  906.      *      2) The current action is one of the other four actions.
  907.      */
  908.  
  909.     if (oldActionPtr != (Sig_Action *) USER_NIL) {
  910.     if (procPtr->sigActions[sigNum] > SIG_NUM_ACTIONS) {
  911.         action.action = SIG_HANDLE_ACTION;
  912.         action.handler = (int (*)())procPtr->sigActions[sigNum];
  913.         action.sigHoldMask = procPtr->sigMasks[sigNum];
  914.     } else {
  915.         if (procPtr->sigActions[sigNum] == sigDefActions[sigNum]) {
  916.         action.action = SIG_DEFAULT_ACTION;
  917.         } else {
  918.         action.action = procPtr->sigActions[sigNum];
  919.         }
  920.     }
  921.     if (Vm_CopyOut(sizeof(action), (Address) &action, 
  922.         (Address) oldActionPtr) != SUCCESS) {
  923.         return(SYS_ARG_NOACCESS);
  924.     }
  925.     }
  926.  
  927.     /*
  928.      * Copy in the action to take.
  929.      */
  930.  
  931.     if (Vm_CopyIn(sizeof(action), (Address) newActionPtr, 
  932.         (Address) &action) != SUCCESS) {
  933.     return(SYS_ARG_NOACCESS);
  934.     }
  935.  
  936.     /*
  937.      * Make sure that the action is valid.
  938.      */
  939.  
  940.     if (action.action < 0 || action.action > SIG_NUM_ACTIONS) {
  941.     return(SIG_INVALID_ACTION);
  942.     }
  943.  
  944.     if (action.action == SIG_DEFAULT_ACTION) {
  945.     action.action = sigDefActions[sigNum];
  946.     }
  947.  
  948.     /*
  949.      * Store the action.  If it is SIG_HANDLE_ACTION then the handler is stored
  950.      * in place of the action.
  951.      */
  952.  
  953.     if (action.action == SIG_HANDLE_ACTION) {
  954.     if (Vm_CopyIn(4, (Address) ((unsigned int) (action.handler)), 
  955.             (Address) &dummy) != SUCCESS) {
  956.         return(SYS_ARG_NOACCESS);
  957.     }
  958.     procPtr->sigMasks[sigNum] = 
  959.         (sigBitMasks[sigNum] | action.sigHoldMask) & sigCanHoldMask;
  960.     procPtr->sigActions[sigNum] = (unsigned int) action.handler;
  961.     } else if (action.action == SIG_IGNORE_ACTION) {
  962.  
  963.     /*
  964.      * Only actions that can be blocked can be ignored.  This prevents a
  965.      * user from ignoring a signal such as a bus error which would cause
  966.      * the process to take a bus error repeatedly.
  967.      */
  968.  
  969.     if (sigBitMasks[sigNum] & sigCanHoldMask) {
  970.         procPtr->sigActions[sigNum] = SIG_IGNORE_ACTION;
  971.         SigClearPendingMask(procPtr, sigNum);
  972.     } else {
  973.         return(SIG_INVALID_SIGNAL);
  974.     }
  975.     procPtr->sigMasks[sigNum] = 0;
  976.     } else {
  977.     procPtr->sigActions[sigNum] = action.action;
  978.     procPtr->sigMasks[sigNum] = 0;
  979.     }
  980.  
  981.     return(SUCCESS);
  982. }
  983.  
  984.  
  985. /*
  986.  *----------------------------------------------------------------------
  987.  *
  988.  * Sig_Pause --
  989.  *
  990.  *    Atomically change signal hold mask and wait for a signal to arrive.
  991.  *
  992.  * Results:
  993.  *    None.
  994.  *
  995.  * Side effects:
  996.  *    None.
  997.  *
  998.  *----------------------------------------------------------------------
  999.  */
  1000.  
  1001. ENTRY ReturnStatus    
  1002. Sig_Pause(sigHoldMask)
  1003.     int    sigHoldMask;    /* The value that the mask of held signals is to be set
  1004.                to while waiting for a signal to arrive. */
  1005. {
  1006.     register    Proc_ControlBlock    *procPtr;
  1007.     ReturnStatus status;
  1008.     int migMask;
  1009.  
  1010.     LOCK_MONITOR;
  1011.  
  1012.     procPtr = Proc_GetActualProc();
  1013.  
  1014.     /*
  1015.      * The signal mask cannot be restored until the signal handler has
  1016.      * had a chance to be called for the signal that caused Sig_Pause
  1017.      * to return.  To allow this the current hold mask is stored in the
  1018.      * proc table and the flag sigPause is set to be true to indicate that
  1019.      * the hold mask has to be restored after the signal handler has had a
  1020.      * chance to be called.
  1021.      */
  1022.  
  1023.     procPtr->oldSigHoldMask = procPtr->sigHoldMask;
  1024.     procPtr->sigFlags |= SIG_PAUSE_IN_PROGRESS;
  1025.     procPtr->sigHoldMask = sigHoldMask & sigCanHoldMask;
  1026.     procPtr->specialHandling = 1;
  1027.  
  1028.     /*
  1029.      * Wait on the signal condition.  As it turns out since a signal
  1030.      * wakes up the process regardless what it is sleeping on, this condition
  1031.      * variable is never broadcasted on, but we have to wait on something in 
  1032.      * order to release the monitor lock.
  1033.      *
  1034.      * Don't let a Sig_Pause be interrupted by a migrate trap signal.
  1035.      * So, if none of the signal bits are set besides migration-related
  1036.      * signals, and a migration-related signal bit is set, let the user-level
  1037.      * code retry  the signal.
  1038.      */
  1039.     (void) Sync_Wait(&signalCondition, TRUE);
  1040.  
  1041.     migMask = (SigGetBitMask(SIG_MIGRATE_TRAP)) |
  1042.     (SigGetBitMask(SIG_MIGRATE_HOME));
  1043.     if ((! (procPtr->sigPendingMask & ~migMask)) &&
  1044.     (procPtr->sigPendingMask & migMask)) {
  1045.     status = GEN_ABORTED_BY_SIGNAL;
  1046.     } else {
  1047.     status = SUCCESS;
  1048.     }
  1049.     
  1050.     UNLOCK_MONITOR;
  1051.  
  1052.     return(status);
  1053. }
  1054.  
  1055.  
  1056. /*
  1057.  *----------------------------------------------------------------------
  1058.  *
  1059.  * SigClearPendingMask --
  1060.  *
  1061.  *    Remove the given signal from the pending mask.
  1062.  *
  1063.  * Results:
  1064.  *    None.
  1065.  *
  1066.  * Side effects:
  1067.  *    Pending mask for process modified.
  1068.  *
  1069.  *----------------------------------------------------------------------
  1070.  */
  1071.  
  1072. ENTRY void
  1073. SigClearPendingMask(procPtr, sigNum)
  1074.     register    Proc_ControlBlock    *procPtr;
  1075.     int                    sigNum;
  1076. {
  1077.     LOCK_MONITOR;
  1078.  
  1079.     procPtr->sigPendingMask &= ~sigBitMasks[sigNum];
  1080.  
  1081.     UNLOCK_MONITOR;
  1082. }
  1083.  
  1084. /*
  1085.  *---------------------------------------------------------------------------
  1086.  *
  1087.  * Routines for signal handlers --
  1088.  *
  1089.  * A signal handler is called right before a process is to return to 
  1090.  * user space.  In order to do this the current state before the signal
  1091.  * is taken must be saved, the signal handler called, and then the state
  1092.  * restored when the signal handler returns.  It is this modules responsibility
  1093.  * to handle the signal state;  all of the actual saving and restoring of
  1094.  * machine state and the calling of the handler is done in the machine
  1095.  * dependent routines in the mach module.
  1096.  */
  1097.  
  1098.  
  1099. /*
  1100.  *----------------------------------------------------------------------
  1101.  *
  1102.  * Sig_Handle --
  1103.  *
  1104.  *    Set things up so that the signal handler is called for one of the
  1105.  *    signals that are pending for the current process.  This is done
  1106.  *    by saving the old trap stack and modifying the current one.
  1107.  *
  1108.  * Results:
  1109.  *    Return TRUE if a signal is setup to be handled by the user.
  1110.  *
  1111.  * Side effects:
  1112.  *    *trapStackPtr is modified and also the user stack is modified.
  1113.  *
  1114.  *----------------------------------------------------------------------
  1115.  */
  1116. Boolean        
  1117. Sig_Handle(procPtr, sigStackPtr, pcPtr)
  1118.     register    Proc_ControlBlock    *procPtr;
  1119.     register    Sig_Stack        *sigStackPtr;
  1120.     Address                *pcPtr;
  1121. {
  1122.     int                    sigs;
  1123.     int                    sigNum;
  1124.     unsigned    int            *bitMaskPtr;
  1125.     int                    sigBitMask;
  1126.  
  1127.     /*
  1128.      * Find out which signals are pending.
  1129.      */
  1130.     sigs = procPtr->sigPendingMask & ~procPtr->sigHoldMask;
  1131.     if (sigs == 0) {
  1132.     return(FALSE);
  1133.     }
  1134.  
  1135.     /*
  1136.      * Check for the signal SIG_KILL.  This is processed specially because
  1137.      * it is how processes that have some problem such as being unable
  1138.      * to write to swap space on the file server are destroyed.
  1139.      */
  1140.     if (sigs & sigBitMasks[SIG_KILL]) {
  1141.     if (procPtr->sigCodes[SIG_KILL] != SIG_NO_CODE) {
  1142.         Proc_ExitInt(PROC_TERM_DESTROYED, 
  1143.             procPtr->sigCodes[SIG_KILL], 0);
  1144.     } else {
  1145.         Proc_ExitInt(PROC_TERM_SIGNALED, SIG_KILL, 0);
  1146.     }
  1147.     }
  1148.  
  1149.     for (sigNum = SIG_MIN_SIGNAL, bitMaskPtr = &sigBitMasks[SIG_MIN_SIGNAL];
  1150.      !(sigs & *bitMaskPtr);
  1151.      sigNum++, bitMaskPtr++) {
  1152.     }
  1153.  
  1154.     SigClearPendingMask(procPtr, sigNum);
  1155.  
  1156.     /*
  1157.      * Process the signal.
  1158.      */
  1159.     switch (procPtr->sigActions[sigNum]) {
  1160.     case SIG_IGNORE_ACTION:
  1161.         printf("Warning: %s\n",
  1162.         "Sig_Handle:  An ignored signal was in a signal pending mask.");
  1163.         return(FALSE);
  1164.  
  1165.     case SIG_KILL_ACTION:
  1166.         if (sigNum == SIG_KILL || !(procPtr->genFlags & PROC_DEBUGGED)) {
  1167.         Proc_ExitInt(PROC_TERM_SIGNALED, sigNum,
  1168.             procPtr->sigCodes[sigNum]);
  1169.         panic("Sig_Handle: Proc_Exit returned!\n");
  1170.         } else {
  1171.         /* Fall through */
  1172.         }
  1173.  
  1174.     case SIG_SUSPEND_ACTION:
  1175.     case SIG_DEBUG_ACTION:
  1176.         /* 
  1177.          * A suspended process and a debugged process are basically
  1178.          * the same.  A suspended process can be debugged just like
  1179.          * a process in the debug state.   The only difference is that
  1180.          * a suspended process does not go onto the debug list; it can
  1181.          * only be debugged by a debugger that specifically asks for
  1182.          * it.
  1183.          *
  1184.          * Suspend the process.
  1185.          */
  1186.         Proc_SuspendProcess(procPtr,
  1187.             procPtr->sigActions[sigNum] == SIG_DEBUG_ACTION,
  1188.             PROC_TERM_SIGNALED, sigNum, 
  1189.             procPtr->sigCodes[sigNum]);
  1190.         return(FALSE);
  1191.  
  1192.     case SIG_MIGRATE_ACTION:
  1193.         /*
  1194.          * If the process was in the middle of a page fault,
  1195.          * its PC in the trap stack is not useable.
  1196.          * Reset the pending condition but hold it until we get out of
  1197.          * the kernel.
  1198.          */
  1199.         if (!Mach_CanMigrate(procPtr)) {
  1200.         LocalSend(procPtr, sigNum, procPtr->sigCodes[sigNum],
  1201.             (Address)procPtr->sigAddr);
  1202.         procPtr->sigHoldMask |= SigGetBitMask(SIG_MIGRATE_TRAP);
  1203.         return(FALSE);
  1204.         }
  1205.  
  1206.         /*
  1207.          * Double-check against process not allowed to migrate.  This
  1208.          * can happen if a process migrates, opens a pdev as master,
  1209.          * and gets signalled to migrate home.
  1210.          */
  1211.         if (procPtr->genFlags & PROC_DONT_MIGRATE) {
  1212.         if (proc_MigDebugLevel > 0) {
  1213.             printf("Proc_Migrate: process %x is not allowed to migrate.\n",
  1214.                    procPtr->processID);
  1215.         }
  1216.         return(FALSE);
  1217.         }
  1218.         if (procPtr->peerHostID != NIL) {
  1219.         if (proc_MigDebugLevel > 6) {
  1220.             printf("Sig_Handle calling Proc_MigrateTrap for process %x.\n",
  1221.                    procPtr->processID);
  1222.         }
  1223.         Proc_MigrateTrap(procPtr);
  1224.         }
  1225.         return(FALSE);
  1226.  
  1227.     case SIG_DEFAULT_ACTION:
  1228.         panic("Sig_Handle: SIG_DEFAULT_ACTION found in array of actions?\n");
  1229.     }
  1230.  
  1231.     /*
  1232.      * Set up our part of the signal stack.
  1233.      */
  1234.     sigStackPtr->sigNum = sigNum;
  1235.     sigStackPtr->sigCode = procPtr->sigCodes[sigNum];
  1236.     sigStackPtr->sigAddr = procPtr->sigAddr;
  1237.     /*
  1238.      * If this signal handler is being called after a call to Sig_Pause then
  1239.      * the real signal hold mask has to be restored after the handler returns.
  1240.      * This is assured by pushing the real hold mask which is stored in 
  1241.      * the proc table onto the stack.
  1242.      */
  1243.     if (procPtr->sigFlags & SIG_PAUSE_IN_PROGRESS) {
  1244.     procPtr->sigFlags &= ~SIG_PAUSE_IN_PROGRESS;
  1245.     sigStackPtr->contextPtr->oldHoldMask = procPtr->oldSigHoldMask;
  1246.     } else {
  1247.     sigStackPtr->contextPtr->oldHoldMask = procPtr->sigHoldMask;
  1248.     }
  1249.  
  1250.     procPtr->sigHoldMask |= procPtr->sigMasks[sigNum];
  1251.     sigBitMask = sigBitMasks[sigNum];
  1252.     if (sigBitMask & ~sigCanHoldMask) {
  1253.     /*
  1254.      * If this is a non-blockable signal then add it to the hold mask
  1255.      * so that if we get it again we know that it can't be handled.
  1256.      */
  1257.     procPtr->sigHoldMask |= sigBitMask;
  1258.     }
  1259.     procPtr->specialHandling = 1;
  1260.     *pcPtr = (Address)procPtr->sigActions[sigNum];
  1261.     return(TRUE);
  1262. }
  1263.  
  1264.  
  1265. /*
  1266.  *----------------------------------------------------------------------
  1267.  *
  1268.  * Sig_Return --
  1269.  *
  1270.  *    Process a return from signal.
  1271.  *
  1272.  * Results:
  1273.  *    None.
  1274.  *
  1275.  * Side effects:
  1276.  *    The trap stack is modified.
  1277.  *
  1278.  *----------------------------------------------------------------------
  1279.  */
  1280. void        
  1281. Sig_Return(procPtr, sigStackPtr)
  1282.     register Proc_ControlBlock    *procPtr;    /* Process that is returning
  1283.                          * from a signal. */
  1284.     Sig_Stack            *sigStackPtr;    /* Signal stack. */
  1285. {
  1286.     procPtr->sigHoldMask = sigStackPtr->contextPtr->oldHoldMask;
  1287.     procPtr->specialHandling = 1;
  1288. }
  1289.  
  1290.  
  1291. /*
  1292.  *----------------------------------------------------------------------
  1293.  *
  1294.  * Sig_AllowMigration --
  1295.  *
  1296.  *    Set up a process to allow migration.  This is a special call
  1297.  *    because normally the SIG_MIGRATE_TRAP signal is not holdable in
  1298.  *    the first place.
  1299.  *
  1300.  *    This could be a macro and be called directly from
  1301.  *    Mach_StartUserProc, once things are stable....
  1302.  *
  1303.  * Results:
  1304.  *    None.
  1305.  *
  1306.  * Side effects:
  1307.  *    The process's hold mask is modified.
  1308.  *
  1309.  *----------------------------------------------------------------------
  1310.  */
  1311. void        
  1312. Sig_AllowMigration(procPtr)
  1313.     register Proc_ControlBlock    *procPtr;    /* process to modify */
  1314. {
  1315.     if (procPtr->sigHoldMask &&
  1316.     (procPtr->sigHoldMask & sigBitMasks[SIG_MIGRATE_TRAP])) {
  1317.         procPtr->sigHoldMask &= ~sigBitMasks[SIG_MIGRATE_TRAP];
  1318.     procPtr->specialHandling = 1;
  1319.     }
  1320. }
  1321.  
  1322.  
  1323. /*
  1324.  *----------------------------------------------------------------------
  1325.  *
  1326.  * Sig_CheckForKill --
  1327.  *
  1328.  *    Check if a process has a kill signal and kill it if so.
  1329.  *    Otherwise return.  this is for calling in difficult places where
  1330.  *    we can't allow any signals that would be handled in user mode to
  1331.  *    occur.
  1332.  *
  1333.  * Results:
  1334.  *    None.
  1335.  *
  1336.  * Side effects:
  1337.  *    Process may be killed.
  1338.  *
  1339.  *----------------------------------------------------------------------
  1340.  */
  1341. void        
  1342. Sig_CheckForKill(procPtr)
  1343.     Proc_ControlBlock    *procPtr;
  1344. {
  1345.     int                    sigs;
  1346.  
  1347.     /*
  1348.      * Find out which signals are pending.
  1349.      */
  1350.     sigs = procPtr->sigPendingMask & ~procPtr->sigHoldMask;
  1351.     if (sigs == 0) {
  1352.     return;
  1353.     }
  1354.  
  1355.     /*
  1356.      * Check for the signal SIG_KILL.  This is processed specially because
  1357.      * it is how processes that have some problem such as being unable
  1358.      * to write to swap space on the file server are destroyed.
  1359.      */
  1360.     if (sigs & sigBitMasks[SIG_KILL]) {
  1361.     if (procPtr->sigCodes[SIG_KILL] != SIG_NO_CODE) {
  1362.         Proc_ExitInt(PROC_TERM_DESTROYED, 
  1363.             procPtr->sigCodes[SIG_KILL], 0);
  1364.     } else {
  1365.         Proc_ExitInt(PROC_TERM_SIGNALED, SIG_KILL, 0);
  1366.     }
  1367.     }
  1368.     return;
  1369. }
  1370.